package com.ciwong.epaper.modules.epaper.ui.autonomicstudy;

import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;


/**
 * Created by keepon on 2018/2/1.
 */

public class AutonomicCase extends RelativeLayout implements View.OnClickListener {
	private final SimpleDraweeView mIvBook;
	private final TextView mTvBookName;
	private final ImageView mIvKaoShi;
	private final ImageView mIvFree;
	private Fragment mFragment;
	private  float bookWidth;
	private  float bookHeight;
	private int itemWidth;
	private Uri uri;
	private static final String     TAG                 = "AutonomicCase";
	private              EpaperInfo mAutonomicStudyBean = new EpaperInfo();

	public AutonomicCase(Context context, Fragment fragment, float width, float height) {
		super(context);
		itemWidth = getResources().getDisplayMetrics().widthPixels;
		bookWidth = width;
		bookHeight = height;
		LayoutInflater.from(this.getContext()).inflate(R.layout.item_autonomic_book_case, this);

		mIvBook = (SimpleDraweeView) findViewById(R.id.iv_book);
		mTvBookName = (TextView) findViewById(R.id.tv_book_name);
		mIvKaoShi = (ImageView) findViewById(R.id.img_kaoshi1);
		mIvFree = (ImageView) findViewById(R.id.img_free1);
		setOnClickListener(this);
	}
	public void bindData(EpaperInfo autonomicStudyBean){
		mAutonomicStudyBean = autonomicStudyBean;

		if(autonomicStudyBean!=null){
			if (!TextUtils.isEmpty(autonomicStudyBean.getCover())) {
				uri = Uri.parse(autonomicStudyBean.getCover());
				setNetImg(mIvBook,uri,mIvBook.getWidth(),mIvBook.getHeight());
			}else {
				setLocalDefaultImg(mIvBook);
			}
			mTvBookName.setText(autonomicStudyBean.getProductName());
			mIvKaoShi.setVisibility(autonomicStudyBean.getRequired() == 1 ? View.VISIBLE : View.INVISIBLE);
			mIvFree.setVisibility(autonomicStudyBean.getIsFree() == 1 ? View.VISIBLE : View.INVISIBLE);

		}
	}
	private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
		ImageRequest request=null;
		if(uri!=null)
		{
			request= ImageRequestBuilder.newBuilderWithSource(uri)
					.setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(getContext(),300),DisplayUtils.dip2px(getContext(),420))).build();

			DraweeController drawweeController= Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
					//列表滑动会变流畅
					.setOldController(simpleDraweeView.getController())
					//                    .setAutoPlayAnimations(true)
					.build();
			simpleDraweeView.setController(drawweeController);
		}

	}
	private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
		Uri uri = Uri.parse("res:///" + R.mipmap.item_load_icon);
		DraweeController controller = Fresco.newDraweeControllerBuilder()
				.setUri(uri)
				.setTapToRetryEnabled(true)
				.build();
		simpleDraweeView.setController(controller);
	}

	@Override
	public void onClick(View v) {
		/*if (null != mAutonomicStudyBean) {
			EpaperJumpManager.jumpToCatalog(R.string.go_back, (Activity) getContext(),
					mAutonomicStudyBean,
					IntentValue.JumpToBookCatalogType.FROM_DESK_BOOK, -1,  mAutonomicStudyBean.getServerId());
		}*/
	}
}
